package com.yangfei.mystudy1020.activity;

import android.app.Activity;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ListView;
import android.widget.SimpleAdapter;

import com.yangfei.mystudy1020.R;
import com.yangfei.mystudy1020.db.DaoMaster;
import com.yangfei.mystudy1020.db.DaoSession;
import com.yangfei.mystudy1020.db.Teacher;
import com.yangfei.mystudy1020.db.TeacherDao;
import com.yangfei.mystudy1020.db.User;
import com.yangfei.mystudy1020.db.UserDao;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class GreenDaoTestActivity extends Activity {

    @BindView(R.id.btn_add)
    Button mBtnAdd;
    @BindView(R.id.btn_delete)
    Button mBtnDelete;
    @BindView(R.id.btn_clear)
    Button mBtnClear;
    @BindView(R.id.lv_data)
    ListView mLvData;
    private DaoMaster.DevOpenHelper mDBHelper;
    private SQLiteDatabase db;
    private DaoMaster mDaoMaster;
    private DaoSession mDaoSession;
    private UserDao mUserDao;
    private TeacherDao mTeacherDao;
    SimpleAdapter mAdapter;
    List<Map<String, Object>> mDataList;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_green_dao_test);
        ButterKnife.bind(this);

        setupDB();
        initList();
    }

    private void setupDB() {
        mDBHelper = new DaoMaster.DevOpenHelper(this, "note-db", null);
        db = mDBHelper.getWritableDatabase();
        mDaoMaster = new DaoMaster(db);
        mDaoSession = mDaoMaster.newSession();
        mUserDao = getDaoSession().getUserDao();
        mTeacherDao = getDaoSession().getTeacherDao();
    }

    public DaoSession getDaoSession() {
        return mDaoSession;
    }

    public SQLiteDatabase getDb() {
        return db;
    }

    @OnClick({R.id.btn_add, R.id.btn_delete, R.id.btn_clear})
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_add: {
                addData();
                break;
            }
            case R.id.btn_delete: {
                deleteData();
                break;
            }
            case R.id.btn_clear: {
                deleteAllData();
                break;
            }

        }
    }

    private void addData() {
        for (int i = 0; i < 10; i++) {

            Teacher t = new Teacher(i,i);
            User mUser = new User(System.currentTimeMillis(), "anye" + i,"school"+i,20+i,i);

            mTeacherDao.insert(t);
            mUserDao.insert(mUser);//添加一个

        }

        List<User> users = mUserDao.queryBuilder().list();
        updateListData();
    }

    private void deleteData() {
        List<User> users = mUserDao.loadAll();
        if (users.size() > 0) {
            mUserDao.delete(users.get(0));
        }
        updateListData();
    }

    private void deleteAllData() {
        mUserDao.deleteAll();
        updateListData();
    }

    private void initList() {
        mDataList = new ArrayList<>();
        getData();
        mAdapter = new SimpleAdapter(this, mDataList, R.layout.data_list,
                new String[]{"title", "info"},
                new int[]{R.id.title, R.id.info});
        mLvData.setAdapter(mAdapter);
    }

    private void getData() {
        mDataList.clear();
        List<User> users = mUserDao.loadAll();
        for(int i=0;i<users.size();i++){
            Map<String, Object> map = new HashMap<String, Object>();
            map.put("title", users.get(i).getId());
            map.put("info", users.get(i).getName());
            mDataList.add(map);
        }
    }

    private void updateListData(){
        getData();
        mAdapter.notifyDataSetChanged();
    }

}
